package com.testitems.config.security;

import com.testitems.enums.RoleEnum;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author GFH
 * @since 2021-11-20
 */
public class RoleHelper {
    public final static Map<RoleEnum, SimpleGrantedAuthority> roles = new HashMap<>();

    static {
        for (RoleEnum value : RoleEnum.values()) {
            roles.put(value, new SimpleGrantedAuthority(value.name()));
        }
    }

    /**
     * 指定用户是否为 超级管理员
     */
    public static boolean hasSuperAdmin(UserInfo userInfo) {
        Objects.requireNonNull(userInfo);
        return userInfo.hasRole(RoleEnum.SUPER_ADMIN);
    }

    /**
     * 指定用户是否是 管理员
     */
    public static boolean hasAdmin(UserInfo userInfo) {
        Objects.requireNonNull(userInfo);
        return userInfo.hasRole(RoleEnum.ADMIN);
    }


    /**
     * 指定用户是否是 普通员工
     */
    public static boolean hasEmployee(UserInfo userInfo) {
        Objects.requireNonNull(userInfo);
        return userInfo.hasRole(RoleEnum.EMPLOYEE);
    }
}
